<?php
class Cms_SearchController extends Zend_Controller_Action {
    private $_languages = array();
    public function init() {
    	$this->_languages = Utils_Global::getSupportLanguages();
    }
    
	public function indexAction() {
		$keySearch = Utils_Global::$params['keySearch'];
		$page = intval(Utils_Global::$params['page']);
		$limit = intval(Utils_Global::$params['limit']);
		$lang = Utils_Global::$params['lang'];
		if(!in_array($lang, $this->_languages)) {
			$lang = Utils_Global::getCurrentLanguage();
		}
		$for = Utils_Global::$params['type'];//1: course, 2: article
		if($page < 1) {
			$page = 1;
		}
		if($limit <= 0){
			$limit = Utils_Global::getConfig('cms', 'site', 'coursePerPage');
		}

		$options = array('content' => $keySearch, 'offset' => ($page - 1)*$limit, 'limit' => $limit, 'lang' => $lang);
		
		if(!Utils_Global::get('isAjax')) {
    		$courseModel = Cms_Model_Course::factory();
    		$courses = $courseModel->getCourses($options);
    		$courseCount = $courseModel->getCourseCount($options);
    		
    		$articleModel = Cms_Model_Article::factory();
    		$articles = $articleModel->getArticles($options);
    		$articleCount = $articleModel->getArticleCount($options);
    		
    		$this->view->courses = $courses;
    		$this->view->articles = $articles;
    		$this->view->keySearch = $keySearch;
    		$this->view->page = $page;
    		$this->view->coursetotalItem = $courseCount;
    		$this->view->articletotalItem = $articleCount;
    		$this->view->numRowPerPage = $limit;
    		$this->view->staticUrlCourse = Utils_Global::get('staticUrl') . '/course/';
    		$this->view->staticUrlArticle = Utils_Global::get('staticUrl') . '/article/';
    		$this->view->readArticle = Utils_Global::getConfig('cms', 'site', 'readArticle');
    		
    		$title = sprintf($this->view->translate('search', $lang), $keySearch);
    		$this->view->metadata = $this->view->metadata('home', array('title_label' => $title),
    				$this->view->serverUrl() . $canonical);
    		
    		$this->view->articleContent = $this->view->render('search/article-list.phtml');
    		$this->view->courseContent = $this->view->render('search/course-list.phtml');
		} else {
	    	$this->_helper->layout()->disableLayout();
		    if($for == 1) {//Course
		        $courseModel = Cms_Model_Course::factory();
		        $courses = $courseModel->getCourses($options);
		        $courseCount = $courseModel->getCourseCount($options);
		        
		        $this->view->courses = $courses;
		        $this->view->keySearch = $keySearch;
		        $this->view->page = $page;
		        $this->view->coursetotalItem = $courseCount;
		        $this->view->numRowPerPage = $limit;
		        $this->view->staticUrlCourse = Utils_Global::get('staticUrl') . '/course/';
		        $this->view->readArticle = Utils_Global::getConfig('cms', 'site', 'readArticle');
    	    	$this->render('course-list');
		    } elseif($for == 2) {//Article
		        $articleModel = Cms_Model_Article::factory();
		        $articles = $articleModel->getArticles($options);
		        $articleCount = $articleModel->getArticleCount($options);
		        $this->view->articles = $articles;
		        $this->view->keySearch = $keySearch;
		        $this->view->page = $page;
		        $this->view->articletotalItem = $articleCount;
		        $this->view->numRowPerPage = $limit;
		        $this->view->staticUrlArticle = Utils_Global::get('staticUrl') . '/article/';
		        $this->view->readArticle = Utils_Global::getConfig('cms', 'site', 'readArticle');
		        $this->render('article-list');
		    }
		};
	}
}
